﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IIControls;
using IIControls.IAControl;
using System.Web.Script.Serialization;

namespace Communicate
{
   public  class RecData
    {

        public static  void ReceiveDataAnaly(byte[] data, string res)
        {
            JavaScriptSerializer jserial = new JavaScriptSerializer();
            Dictionary<string, object> recData = jserial.Deserialize<Dictionary<string, object>>(res);
            if (recData == null)
                return;
            int devicecode = Convert.ToInt32(recData["device_type"]);
            switch (devicecode)
            {
                case 1:
                    switch (Convert.ToInt32(recData["device_cmd"]))
                    {
                        case 49:  //阀门1开
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { ValveControl.ValveState.OPEN, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"] + "1");
                            break;
                        case 50:  //阀门1关
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { ValveControl.ValveState.CLOSE, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"] + "1");
                            break;
                        case 51:  //阀门2开
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { ValveControl.ValveState.OPEN, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"] + "2");
                            break;
                        case 52:  //阀门2关
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { ValveControl.ValveState.CLOSE, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"] + "2");
                            break;
                        case 53:  //阀门3开
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { ValveControl.ValveState.OPEN, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"] + "3");
                            break;
                        case 54:  //阀门3关
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { ValveControl.ValveState.CLOSE, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"] + "3");
                            break;
                        case 55:  //阀门4开
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { ValveControl.ValveState.OPEN, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"] + "4");
                            break;
                        case 56:  //阀门4关
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { ValveControl.ValveState.CLOSE, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"] + "4");
                            break;
                        case 57:  //状态
                            string[] statues = (string[])((System.Collections.ArrayList)recData["list"]).ToArray(typeof(string));
                            for (int i = 0; i < statues.Length; i++)
                            {
                                BusTransfer.MsgTransfer.Instance.Send(new object[] { ValveControl.ValveState.STATUE, Convert.ToInt32(statues[i]) > 0 ? true : false }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"] + (i + 1).ToString());
                            }
                            break;

                        case 58:  //电压                         
                        case 62://58,62两种条件处理方法一致
                            double vol = Convert.ToDouble(recData["retdata"]);
                            for (int i = 0; i < 4; i++)
                            {
                                BusTransfer.MsgTransfer.Instance.Send(new object[] { ValveControl.ValveState.VOLTAGE, vol }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"] + (i + 1).ToString());
                            }
                            break;
                        default:
                            break;
                    }
                    break;
                case 2:
                    break;
                case 3:
                    switch (Convert.ToInt32(recData["device_cmd"]))
                    {
                        case 1:  //读信息
                            break;
                        case 2: //初始化
                            for (int i = 0; i < 4; i++)
                            {
                                BusTransfer.MsgTransfer.Instance.Send(new object[] { ValveControl.ValveState.INIT, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"] + (i + 1).ToString());
                            }
                            break;

                    }
                    break;
                case 4:
                    switch (Convert.ToInt32(recData["device_cmd"]))
                    {
                        case 1:  //通道1开
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.Channel1Open, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;
                        case 2:  //通道1关
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.Channel1Close, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;
                        case 3:  //通道2开
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.Channel2Open, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;
                        case 4:   //通道2关
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.Channel2Close, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;

                        case 5:  //通道3开
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.Channel3Open, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;

                        case 6:  //通道3关
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.Channel3Close, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;

                        case 7:  //闸刀开
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.ValveOpen, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;

                        case 8:  //闸刀关
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.ValveClose, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;

                        case 9: //原井开
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.Pump1Open, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;

                        case 10:  //原井关
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.Pump1Close, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;

                        case 11:  //增压泵开
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.Pump2Open, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;

                        case 12:  //增压泵关
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.Pump2Close, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;
                        case 13:  //检测状态
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.CheckStatue ,recData["list"]}, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;

                        case 14:  //检测压力
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.FrontPress , recData["retdata"] }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;

                        case 16:  //同步日期
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.SyncTime, null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;
                        case 17:  //读取报警信息
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.ReadRecord , recData["list"], recData["explain"] }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;
                        case 18: //清除报警信息
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.ClearRecord,null   }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;
                        case 21:  //读取日期
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.ReadTime , Convert.ToDateTime((string)recData["retdata"]) }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;
                        case 22: //测量电压
                            break;
                        case 23: //报警自动回发
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.AutoAlarm ,  recData["explain"] }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;

                    }
                    break;
                case 5:
                    break;
                case 6:
                    switch (Convert.ToInt32(recData["device_cmd"]))
                    {
                        case 1:  //读信息
                            break;
                        case 2: //初始化
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.PumpInit , null }, "v_" + (string)recData["gprs_code"] + (string)recData["device_code"]);
                            break;
                    }
                    break;
                case 10:
                    switch (Convert.ToInt32(recData["device_cmd"]))
                    {
                        case 1:
                            break;
                        case 11:  //通信失败
                            for (int i = 0; i < 4; i++)
                            {
                                BusTransfer.MsgTransfer.Instance.Send(new object[] { ValveControl.ValveState.ERROR, null }, "v_" + (string)recData["gprs_code"] + (string)recData["retdata"] + (i + 1).ToString());
                            }
                            BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.CommError, null }, "v_" + (string)recData["gprs_code"] + (string)recData["retdata"]);
                            break;
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
